<?php

if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

/**
*
* @package MediaLibrary
* @copyright 2009 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);Rob de Cleen(rob@decleen.com)
* Homepage: http://www.joomlawebserver.com
* @version: 1.5.3 Basic $
*
**/


class mosMedialibraryOthers {

	/**
	 * Sets the parametes for connecting to a webservice via a 
	 * @param the array of parameters
	 * @deprecated $Revision: 1.0 $ - December 2007
	 */
	function setParams()
        {

//echo '2222: ';exit;
	
		global $medialibrary_configuration;
		// Prepare settings
		// Using single quotes is easier because you don't have to escape them. For every setting, add a line.
		$settings = "<?php\n";

		$settings .= "// Do not edit this file. Generated by admin script.\n";
		$settings .= "// MediaLibrary Configuration file\n";
		$settings .= "// General Informations \n";
		$settings .= "\$medialibrary_configuration['release']['version']='" . $medialibrary_configuration['release']['version'] ."';\n";
		$settings .= "\$medialibrary_configuration['release']['date']='" . $medialibrary_configuration['release']['date'] ."';\n";
		$settings .= "// edit book \n";
		$settings .= "\$medialibrary_configuration['editmedia']['check']['isbn_asin']='" . $medialibrary_configuration['editmedia']['check']['isbn_asin']  . "';\n";
		$settings .= "\$medialibrary_configuration['editmedia']['default']['lang']='" . $medialibrary_configuration['editmedia']['default']['lang']  . "';\n";
		$settings .= "// price settings\n";
		$settings .= "\$medialibrary_configuration['price']['show']='" . $medialibrary_configuration['price']['show'] ."';\n";
		$settings .= "// amazon settings\n";
		$settings .= "\$medialibrary_configuration['ws']['amazon']['tag']='" . $medialibrary_configuration['ws']['amazon']['tag'] ."';\n";
		$settings .= "\$medialibrary_configuration['ws']['amazon']['devtag']= '" . $medialibrary_configuration['ws']['amazon']['devtag'] ."';\n";
    $settings .= "// fetch images settings\n";
    $settings .= "\$medialibrary_configuration['fetchImages']['boolean']='" . $medialibrary_configuration['fetchImages']['boolean'] ."';\n";
		$settings .= "\$medialibrary_configuration['fetchImages']['location']='" . $medialibrary_configuration['fetchImages']['location'] ."';\n";
		$settings .= "// review settings\n";
		$settings .= "\$medialibrary_configuration['reviews']['show']='" . $medialibrary_configuration['reviews']['show'] ."';\n";
		$settings .= "\$medialibrary_configuration['reviews']['registrationlevel']='" . $medialibrary_configuration['reviews']['registrationlevel'] ."';\n";		
		$settings .= "// lend settings\n";
		$settings .= "\$medialibrary_configuration['lendstatus']['show']='" . $medialibrary_configuration['lendstatus']['show'] ."';\n";
		$settings .= "\$medialibrary_configuration['lendrequest']['registrationlevel']='" . $medialibrary_configuration['lendrequest']['registrationlevel'] ."';\n";
		$settings .= "// ebook settings\n";
		$settings .= "\$medialibrary_configuration['emedias']['allow']='" . $medialibrary_configuration['emedias']['allow'] ."';\n";
		$settings .= "\$medialibrary_configuration['emedias']['show']='" . $medialibrary_configuration['emedias']['show'] ."';\n";
		$settings .= "\$medialibrary_configuration['emedias']['registrationlevel']='" . $medialibrary_configuration['emedias']['registrationlevel'] ."';\n";
		$settings .= "\$medialibrary_configuration['emedias']['location']='" . $medialibrary_configuration['emedias']['location'] ."';\n";
		$settings .= "// debuging\n";
		$settings .= "\$medialibrary_configuration['debug']='" . $medialibrary_configuration['debug'] . "';\n";
		$settings .= "\$medialibrary_configuration['page']['items']='" . $medialibrary_configuration['page']['items'] . "';\n";
		$settings .= "// foto size\n";
		$settings .= "\$medialibrary_configuration['foto']['high']= '" . $medialibrary_configuration['foto']['high'] ."';\n";
		$settings .= "\$medialibrary_configuration['foto']['width']= '" . $medialibrary_configuration['foto']['width'] ."';\n";


		//add for show in category picture
		$settings .= "// show in category picture\n";
		$settings .= "\$medialibrary_configuration['cat_pic']['show']='" . $medialibrary_configuration['cat_pic']['show'] ."';\n";

                 //add for show subcategory 
                 $settings .= "// show subcategory\n";
                 $settings .= "\$medialibrary_configuration['subcategory']['show']='" . $medialibrary_configuration['subcategory']['show'] ."';\n";
                 
		$settings .= "?>\n";
	

		// Write out new initialization file
		$fd = fopen("./components/com_medialibrary/admin.medialibrary.class.conf.php", "w" )
		or die ("Cannot create configuration file.");
		fwrite( $fd, $settings );
		fclose( $fd );
	}

	/**
	 * Rating Array
	 * @return array an Array containing the information of all Rating
	 * possibilities
	 */
	function getRatingArray(){
		$i = 0;
		$retVal = array();
		while($i < 5){
			//erster teil x,0
			$tmp = $i * 2;
			array_push($retVal, array($tmp, $i.",0"));
			//zweiter teil x,5
			$tmp ++;
			array_push($retVal, array($tmp, $i.",5"));
			$i ++;
		}
		array_push($retVal, array(10, "10,0"));
		return $retVal;
	}
	
	/**
	 * Language Array - defined in the language files!
	 * @return array an Array containing the information of all Language
	 * possibilities
	 */
	function getLanguageArray(){
		$retVal = array();
		array_push($retVal, array("Not specified", _MEDIALIBRARY_LANGUAGE_NOT_USED));
		array_push($retVal, array("English", _MEDIALIBRARY_LANGUAGE_ENG));
		array_push($retVal, array("Dutch", _MEDIALIBRARY_LANGUAGE_DUT));
		array_push($retVal, array("Italian", _MEDIALIBRARY_LANGUAGE_ITA));
		array_push($retVal, array("French", _MEDIALIBRARY_LANGUAGE_FRE));
    array_push($retVal, array("German", _MEDIALIBRARY_LANGUAGE_GER));
    
		array_push($retVal, array("Spanish",_MEDIALIBRARY_LANGUAGE_SPA));
		array_push($retVal, array("Russian", _MEDIALIBRARY_LANGUAGE_RUS));
		
		return $retVal;
	}
	
	//типы мультимедии
	function getTypeArray()
	{
		$type_list = array();
		array_push($type_list, array("select type", _MEDIALIBRARY_LABEL_MEDIA_TYPE_SELECT));
		array_push($type_list, array("book", _MEDIALIBRARY_LABEL_MEDIA_TYPE_BOOK));
		array_push($type_list, array("video", _MEDIALIBRARY_LABEL_MEDIA_TYPE_VIDEO));
		array_push($type_list, array("music", _MEDIALIBRARY_LABEL_MEDIA_TYPE_MUSIC));
		array_push($type_list, array("game", _MEDIALIBRARY_LABEL_MEDIA_TYPE_GAME));
		return $type_list;
	}
	
	/**
	 * fetches the information from the imageURL field of the book and stores
	 * the book with the ISBN number in the defined folder - then overwrites
	 * the information in the imageURL field so that it points to the file
	 * downloaded
	 * @param mosMediaLibrary store the book with all necessary information.
	 * @return null if everything is OK, else return error string
	 */
	function storeImageFile(& $media_parent,& $media, $picture)
	{
		global $database,$mosConfig_live_site, $mosConfig_absolute_path, $medialibrary_configuration;

		$old_pic=$media->imageURL;

//print_r($media);
		//set path for local host images files 
		$is_local_url = false ;
		if(substr($old_pic,0,4) != "http")
		{
			$old_pic = $mosConfig_live_site . '/'. $old_pic;
			$is_local_url = true;
		}

    $id = $media_parent->id;
    if( $id == 0 ) {
    $database->setQuery("select max(id) as id from #__medialibrary");
    $ids = $database->loadObjectList();
    $id = $ids[0]->id + 1 ;
    }
			
		if ($picture != null)
		{
			$new_pic = $id . strrchr($picture['name'], ".");

			if(!move_uploaded_file($picture['tmp_name'], $mosConfig_absolute_path.$medialibrary_configuration['fetchImages']['location'].$new_pic))  
			{
				return "Error moving File!";
			} else 
			{
				$media->imageURL=$medialibrary_configuration['fetchImages']['location'].$new_pic;return null;
			}

		}
    else if( $is_local_url)
		{
			//for local images - do only check			
			if( !($ch = curl_init($old_pic)) ) return "Bad path to image file: $old_pic";

			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_FAILONERROR, 1);
			curl_exec($ch);
			$intReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			curl_close($ch);

			if ($intReturnCode != 200)
			{
				return "Bad path image file";
			}

		}else if( !$is_local_url && strlen(trim($old_pic)) > 0)
		{
			$new_pic = $id . strrchr($media->imageURL, ".") ;

			if( strrchr($new_pic, "/") ) return "Bad path to image file: $media->imageURL";

			if( !($ch = curl_init($old_pic)) ) return "Bad path to image file: $old_pic";


			if($fp = fopen($mosConfig_absolute_path.$medialibrary_configuration['fetchImages']['location'] . $new_pic, "w") )
			{


				curl_setopt($ch, CURLOPT_FILE, $fp);
				curl_setopt($ch, CURLOPT_HEADER, 0);
				curl_setopt($ch, CURLOPT_FAILONERROR, 1);

				curl_exec($ch);
				$intReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
				curl_close($ch);
				fclose($fp);

				if ($intReturnCode != 200)
				{
					unlink($mosConfig_absolute_path .$medialibrary_configuration['fetchImages']['location']
						.$new_pic);	
					return "Bad path image file";
				}
			}
			else
			{
				 return "Folder".$medialibrary_configuration['fetchImages']['location']."dont exist";
				 
			}
		}
		else
		{
		  return "Bad path or access deny to save image file";
		}
	}
} 
?>
